package org.lzy.shop.adapter

import android.content.Context
import android.view.ViewGroup
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import org.lzy.shop.R
import org.lzy.shop.datas.ThemeColorBean
import org.lzy.shop.util.setUrlLocalRound


class ThemeAdapter(
    private val themeColors: List<ThemeColorBean>,
    private val selectedColorId: Int,
    private val onThemeSelected: (ThemeColorBean) -> Unit):
    BaseQuickAdapter<ThemeColorBean, QuickViewHolder>()  {


    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
        return QuickViewHolder(R.layout.item_theme, parent)
    }

    override fun onBindViewHolder(
        holder: QuickViewHolder,
        position: Int,
        item: ThemeColorBean?
    ) {
           val themeColor = themeColors[position]
           holder.setBackgroundColor(R.id.color_circle,themeColor.colorValue)
           setUrlLocalRound(themeColor.colorValue,50f,holder.getView(R.id.color_circle))
           holder.setText(R.id.color_name,themeColor.name)
           // 显示选中状态
           if (themeColor.id == selectedColorId) {
               holder.setGone(R.id.selected_indicator,false)
           }else{
               holder.setGone(R.id.selected_indicator,true)

           }
           // 设置点击事件
           holder.itemView.setOnClickListener {
               onThemeSelected(themeColor)
           }



    }
     //返回主题长度
    override fun getItemCount(items: List<ThemeColorBean>): Int {
        if (themeColors.isNotEmpty()) {
            return themeColors.size
        }
        return 0
    }
}